from itsdangerous import TimedJSONWebSignatureSerializer as TJWSSerializer
from django.conf import settings

#加密
def generate_sign_openid(openid):
#     1.创建TimedJSONWebSignatureSerializer对象
    serializer = TJWSSerializer(secret_key=settings.SECRET_KEY, expires_in=300)
#     2.加密数据
    sign_openid = serializer.dumps({"openid": openid})
#     3.返回数据
    return sign_openid.decode()

#解密
def decode_sign_openid(sign_openid):
    serializer = TJWSSerializer(secret_key=settings.SECRET_KEY, expires_in=300)
    try:
        dict_data = serializer.loads(sign_openid)
    except Exception as e:
        return None
    return dict_data.get("openid")
